package com.zzy.controller;

import com.zzy.pojo.User;
import com.zzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/queryAll")
    public String queryAll(Model model){
        System.out.println("========================Controller层，查询所有用户=================");
        List<User> userList = userService.queryAll();
        model.addAttribute("userList",userList);
        return "user";
    }

    @RequestMapping("/addUser")
    public void addUser(User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("========================Controller层，保存用户=================");
        userService.addUser(user);
        response.sendRedirect(request.getContextPath()+"/user/queryAll");
    }
}
